#include "Sound.h"
#include "SoundID.h"

Sound::Sound():fmod(0), channel(0)
{
	System_Create( &fmod ); //Create the main system object.
	fmod->init(100, FMOD_INIT_NORMAL, 0);	// Initialize FMOD system.

	char* soundInitList[] =
	{
		SELECT_PATH,
		SHOOT_PATH,
		COLLIDE_PATH,
		DESTROY_PATH,
		WIN_PATH,
		DEAD_PATH,
	};
	
	for(int i=0;i<sizeof(soundInitList)/sizeof(soundInitList[0]);++i)
	{
		FMOD::Sound* s;
		fmod->createSound(soundInitList[i], FMOD_DEFAULT, 0, &s);
		sound.add(s);
	}


	char* musicInitList[] =
	{
		MENU_PATH,
		GAME_PATH,
	};

	for(int i=0;i<sizeof(musicInitList)/sizeof(musicInitList[0]);++i)
	{
		FMOD::Sound* m;
		fmod->createStream(musicInitList[i], FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, &m);
		music.add(m);
	}
}

Sound::~Sound()
{
	for(int i=0;i<sound.Size();++i)
		sound.get(i)->release();
	fmod->release();
}

void Sound::PlayMusic(int ID)
{
	if(musicPlaying)
		channel->stop();
	ID--;
	fmod->playSound(FMOD_CHANNEL_FREE, music.get(ID), true, &channel);
	musicPlaying = false;
	ToggleStream();
}

void Sound::ToggleStream()
{
	channel->setPaused(musicPlaying);
	musicPlaying = !musicPlaying;
}

void Sound::PlaySound(int ID)
{
	ID--;
	fmod->playSound(FMOD_CHANNEL_FREE, sound.get(ID), false, 0);
}